package cn.tedu.pdfamily.product.controller;

import cn.tedu.pdfamily.product.pojo.dto.BannerAddNewDTO;
import cn.tedu.pdfamily.product.pojo.dto.BannerUpdateDTO;
import cn.tedu.pdfamily.product.pojo.vo.BannerListItemVO;
import cn.tedu.pdfamily.product.pojo.vo.BannerStandardVO;
import cn.tedu.pdfamily.product.service.IBannerService;
import cn.tedu.pdfamily.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理轮播图相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/banners")
@Api(tags = "01. 轮播图管理模块")
public class BannerController {
    @Autowired
    IBannerService bannerService;

    public BannerController() {
        log.debug("创建控制器对象:bannerController");
    }

    // http://localhost:9080/banners/add-new
    @ApiOperation("添加轮播图")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid BannerAddNewDTO bannerAddNewDTO) {
        log.debug("开始处理【添加轮播图】的请求，参数：{}", bannerAddNewDTO);
        bannerService.addNew(bannerAddNewDTO);
        log.debug("添加轮播图成功！");
        return JsonResult.ok();
    }

    // http://localhost:9080/banners/9527/delete
    @ApiOperation("根据id删除轮播图")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "轮播图id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除轮播图失败，尝试删除的轮播图的ID无效！") @PathVariable Long id) {
        log.debug("开始处理【根据id删除轮播图】的请求，参数：{}", id);
        bannerService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/banners/9527/update
    @ApiOperation("修改轮播图详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "轮播图id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, BannerUpdateDTO bannerUpdateDTO) {
        log.debug("开始处理【修改轮播图详情】的请求，参数ID：{}, 新数据：{}", id, bannerUpdateDTO);
        bannerService.updateInfoById(id, bannerUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/banners/9527
    @ApiOperation("根据id查询轮播图详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "轮播图id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<BannerStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询轮播图详情】的请求，参数：{}", id);
        BannerStandardVO banner = bannerService.getStandardById(id);
        return JsonResult.ok(banner);
    }

    // http://localhost:9080/banners
    @ApiOperation("查询轮播图列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<BannerListItemVO>> list() {
        log.debug("开始处理【查询轮播图列表】的请求，无参数");
        List<BannerListItemVO> list = bannerService.list();
        return JsonResult.ok(list);
    }
}
